package com.ww.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.*;
import java.util.Properties;

public class DBUtil {
    //驱动类名称
    private static String driverClassName;
    //用户名
    private static String username;
    //密码
    private static String password;
    //连接地址
    private static String url;

    //通过静态代码块加载驱动 加载一次，多次使用
    static {
        Properties p = new Properties();
        //通过类加载器加载文件路径
        String path = DBUtil.class.getClassLoader().
                getResource("jdbc.properties")
                .getPath();
        try {
            p.load(new FileInputStream(new File(path)));
            driverClassName = p.getProperty("jdbc.driverClassName").trim();
            url = p.getProperty("jdbc.url").trim();
            username = p.getProperty("jdbc.username").trim();
            password = p.getProperty("jdbc.password").trim();

        } catch (IOException e) {
            e.printStackTrace();
        }

        try {
            Class.forName(driverClassName);
        } catch (ClassNotFoundException e) {
            System.out.println("驱动加载失败" + e.getMessage());
            e.printStackTrace();
        }

    }


    /**
     * 获取数据库连接
     *
     * @return
     */
    public static Connection getConnection() {
        Connection conn = null;
        try {
            conn = DriverManager.getConnection(url, username, password);
        } catch (SQLException e) {
            System.out.println("数据库连接信息错误" + e.getMessage());
            e.printStackTrace();
        }
        return conn;
    }

    /**
     * 释放连接
     *
     * @param pstm
     * @param conn
     */
    public static void
    close(PreparedStatement pstm,
          Connection conn) {
        try {
            pstm.close();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            if (conn != null) {
                try {
                    pstm.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }

        try {
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static void
    close(ResultSet rs,
          PreparedStatement pstm,
          Connection conn) {

        try {
            rs.close();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            if (rs != null) {
                try {
                    rs.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }

        close(pstm, conn);  //方法调用

    }


    //测试连接
    public static void main(String[] args) {
        Connection conn = DBUtil.getConnection();
        System.out.println(conn);
    }


}


